package com.bestcem.xm.user.grpc.service;

import cn.hutool.core.lang.ObjectId;
import com.alibaba.excel.util.StringUtils;
import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.grpc.service.converter.CommonConverter;
import com.bestcem.xm.user.service.UserAuthService;
import com.bestcem.xm.user.service.UserService;
import com.bestcem.xm.user.service.dto.user.UserLoginDTO;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 用户中心 飞书 相关grpc服务
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/24 13:57
 */
@GrpcService
public class UserFeiShuGrpcService extends UserFeiShuServiceGrpc.UserFeiShuServiceImplBase {

    @Resource
    private UserAuthService userAuthService;

    @Resource
    private UserService userService;

    /**
     * 签发token
     */
    @Override
    public void getToken(GetTokenRequest request, StreamObserver<GetTokenResponse> responseObserver) {
        GetTokenResponse.Builder builder = GetTokenResponse.newBuilder();
        String userId = request.getUserId();
        if (!ObjectId.isValid(userId)) {
            builder.setStatus(RpcStatusResult.error("用户id格式错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<UserLoginDTO> serviceResult = userAuthService.feishuGetToken(userId);
        if (!serviceResult.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        UserLoginDTO userLogin = serviceResult.getData();
        if (ObjectUtils.isEmpty(userLogin.getToken())
                || ObjectUtils.isEmpty(userLogin.getRefreshToken())
                || ObjectUtils.isEmpty(userLogin.getOrgCode())) {
            builder.setStatus(RpcStatusResult.error("Token生成失败"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        builder.setToken(userLogin.getToken());
        builder.setRefreshToken(userLogin.getRefreshToken());
        builder.setOrgCode(userLogin.getOrgCode());
        builder.setGrowingio(CommonConverter.growingioDto2Grpc(userLogin.getGrowingio()));

        builder.setStatus(RpcStatusResult.success());
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 飞书解绑
     *
     * @param request          请求信息
     * @param responseObserver 响应内容
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/29 16:09
     */
    @Override
    public void unBindFeishu(UnBindFeishuRequest request, StreamObserver<UnBindFeishuResponse> responseObserver) {
        // 获取返回对象的类型
        UnBindFeishuResponse.Builder builder = UnBindFeishuResponse.newBuilder();

        // 参数校验
        String orgId = request.getOrgId();
        String openId = request.getOpenId();
        if (StringUtils.isBlank(orgId) && StringUtils.isBlank(openId)) {
            builder.setStatus(RpcStatusResult.error("orgId和openId不能都为空"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        // 解绑企业微信
        ServiceResult<Void> result = userService.unbindFeishu(orgId, openId);
        if (!result.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        } else {
            builder.setStatus(RpcStatusResult.success());
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

}
